<?php  
class Model_Mobile_User extends Model{  

    public function register($user_name,$user_email,$password){

      $arr=array();

      try{

        $total= DB::select(array(DB::expr('COUNT(1)'), 'total'))

        ->from('site_user')->where("fullname",'=',$user_name)->limit(1)->execute()->get("total");
      
        if($total=="0"){

          $total= DB::select(array(DB::expr('COUNT(1)'), 'total'))

          ->from('site_user')->where("user_email",'=',$user_email)->limit(1)->execute()->get("total");
          
          if($total=="0"){


            Database::instance()->begin();
            
            list($insert_id, $total_rows)= DB::insert('site_user')->columns(array('fullname','user_email','user_pwd','createdon'))

            ->values(array($user_name,$user_email,sha1($password),date("Y-m-d h:i:sa")))->execute();
              
            Database::instance()->commit();

            $arr['code']="0x00000";

            $arr['user_name']=$user_name;

            $user=array(
              'userid'=>$insert_id,
              'username'=>$user_name
            );

            Session::instance()->set('user',$user);

            return  json_encode($arr);

          }
          else{

            $arr['code']="0x00001";

            $arr['error_info']=array('register_email'=>'Sorry,the email is exist,please try other email!');

            return  json_encode($arr);

          }
        }
        else{

          $arr['code']="0x00001";

          $arr['error_info']=array('username'=>'Sorry,the username is exist!');

          return  json_encode($arr);

        }

      }

      catch(Exception $e){

        Database::instance()->rollback();

        return json_encode(array("code"=>"0x0000x","msg"=>'Server is busy!'));
      }
    	
    }
   public function user_login($user_email,$user_pwd){

       try{

          $count=DB::select('site_userid')->from('site_user')
          ->where('user_email', '=', $user_email)->limit(1)->execute()->count();

          if($count>0){

              $query=DB::select('site_userid','fullname','user_email','nickname')
              ->from('site_user')->where('user_email', '=', $user_email)
              ->and_where('user_pwd', '=', $user_pwd)->limit(1)->execute();

              $count=$query->count();

              if($count>0){

                  $userid=$query->get("site_userid");

                  $arr= array('code'=>'0x00000');

                  $arr['user_name']=$query->get("fullname");

                  return json_encode($arr);

              }
              else{

                return json_encode(array("code"=>"0x00001","msg"=>'The  password you entered is incorrect.'));

              }
          }
          else{

             return json_encode(array("code"=>"0x00001","msg"=>'The email address  you entered is incorrect.'));

          }
      }
      catch(Exception $e){

        Database::instance()->rollback();

        return json_encode(array("code"=>"0x0000x","msg"=>'Server is busy!'));

      }

   }

   public function saveProfile($firstname, $lastname,$new_password,$old_password){

      $user=Session::instance()->get('user');

      if(empty($user)){

         return json_encode(array(

            'msg'=>"Please login!"

        ));

      }

      $userid=$user['userid'];

      if(!empty($new_password)){

        if(empty($old_password)){

           return json_encode(array(

              'msg'=>"Old Password is requied"

            ));
        }
        
        $count=DB::select('site_userid')->from('site_user')->where('user_pwd', '=', sha1($old_password))->execute()->count();

        if($count>0){

            $pwd=sha1($new_password);

            $sql="update site_user
            set first_name='$firstname',last_name='$lastname',user_pwd='$pwd'
            where site_userid='$userid'";

            $total_rows=DB::query(null,$sql)->execute();
        }
        else{

          return json_encode(array(
              'msg'=>"Old Password is not correct"
          ));

        }
      }
      else{

        $sql="update site_user
            set first_name='$firstname',last_name='$lastname'
            where site_userid='$userid'";

        $total_rows=DB::query(null,$sql)->execute();

      }
      return json_encode(array(

        'code'=>'0x00000',

        'msg'=>"Thanks, your account infomation has been saved"

      ));
   }

}
